<?php

class Empeval_Methods_Request_AddBonusGoalsFormRow {
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	public $bonusGoalsFormId;
	public $type;
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->bonusGoalsFormId = (int)$params->bonusGoalsFormId;
		$this->type = $params->type;
	}
	
	public function process() {
		$bonusGoalsForm = EmpevalBonusGoalsFormsQuery::create()
		->filterById($this->bonusGoalsFormId)
		->filterByEditorId(Empeval_Auth::getInstance()->getUser()->getId())
		->findOne();
		
		if (!($bonusGoalsForm instanceof EmpevalBonusGoalsForms))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
		
		switch ($this->type) {
			case 'kpi':
				$indicator = EmpevalIndicatorsQuery::create()->findOne();
				$kpi = new EmpevalKpi();
				$kpi->setBonusGoalsFormId($bonusGoalsForm->getId());
				$kpi->setTrend('higher');
				$kpi->setIndicators($indicator);
				$kpi->setWeight(10);
				$kpi->setStatus(1);
				$kpi->save();
				$return = new Empeval_Methods_Data_Kpi($kpi);
				break;
			case 'task':
				$tg = new EmpevalTaskGoals();
				$tg->setBonusGoalsFormId($bonusGoalsForm->getId());
				$tg->setWeight(10);
				$tg->setStatus(1);
				$tg->save();
				$return = new Empeval_Methods_Data_TaskGoals($tg);
				break;
			case 'mboF':
				$mbo = new EmpevalMboGoals();
				$mbo->setBonusGoalsFormId($bonusGoalsForm->getId());
				$mbo->setWeightingFactor(10);
				$mbo->setType(0);
				$mbo->setStatus(1);
				$mbo->save();
				$return = new Empeval_Methods_Data_MboGoals($mbo);
				break;
			case 'mboP':
				$mbo = new EmpevalMboGoals();
				$mbo->setBonusGoalsFormId($bonusGoalsForm->getId());
				$mbo->setWeightingFactor(10);
				$mbo->setType(1);
				$mbo->setStatus(1);
				$mbo->save();
				$return = new Empeval_Methods_Data_MboGoals($mbo);
				break;
		}
		
		return new Empeval_Methods_Response_AddBonusGoalsFormRow(true, $return);
	}
	
}